home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 SRC / Mac / Demo / example2 / InterslipControl-2.py next >
Text File  |  1996-04-18  |  3KB  |  117 lines

  1. import FrameWork
  2. import EasyDialogs
  3. import Res
  4. import Dlg
  5. import sys
  6. import interslip
  7. #
  8. # Definitions for our resources
  9. ID_MAIN=512
  10. ID_ABOUT=513
  11.  
  12. ITEM_CONNECT=1
  13. ITEM_DISCONNECT=2
  14. ITEM_STATUS=3
  15. ITEM_MESSAGE=4
  16.  
  17. status2text = ["<idle>", "<wait-modem>", "<dialling>", "<logging in>", 
  18.     "<connected>", "<disconnecting>"]
  19.  
  20. def main():
  21.     try:
  22.         interslip.open()
  23.     except interslip.error, arg:
  24.         EasyDialogs.Message("Cannot open interslip: "+arg[1])
  25.         sys.exit(1)    
  26.     try:
  27.         dummy = Res.GetResource('DLOG', ID_MAIN)
  28.     except Res.Error:
  29.         try:
  30.             Res.OpenResFile("InterslipControl-2.rsrc")
  31.         except Res.error:
  32.             EasyDialogs.Message("Cannot open InterslipControl-2.rsrc: "+arg[1])
  33.             sys.exit(1)    
  34.     InterslipControl()
  35.     
  36. class InterslipControl(FrameWork.Application):
  37.     "Application class for InterslipControl"
  38.     
  39.     def __init__(self):
  40.         # First init menus, etc.
  41.         FrameWork.Application.__init__(self)
  42.         # Next create our dialog
  43.         self.main_dialog = MyDialog(self)
  44.         # Now open the dialog
  45.         self.main_dialog.open(ID_MAIN)
  46.         # Finally, go into the event loop
  47.         self.mainloop()
  48.     
  49.     def makeusermenus(self):
  50.         self.filemenu = m = FrameWork.Menu(self.menubar, "File")
  51.         self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
  52.     
  53.     def quit(self, *args):
  54.         raise self
  55.         
  56.     def do_about(self, *args):
  57.         f = Dlg.GetNewDialog(ID_ABOUT, -1)
  58.         while 1:
  59.             n = Dlg.ModalDialog(None)
  60.             if n == 1:
  61.                 return
  62.                 
  63.     def idle(self, event):
  64.         "Idle routine - update status"
  65.         self.main_dialog.updatestatus()
  66.         
  67. class MyDialog(FrameWork.DialogWindow):
  68.     "Main dialog window for InterslipControl"
  69.     def __init__(self, parent):
  70.         FrameWork.DialogWindow.__init__(self, parent)
  71.         self.last_status = None
  72.         self.last_msgnum = None
  73.     
  74.     def do_itemhit(self, item, event):
  75.         if item == ITEM_DISCONNECT:
  76.             self.disconnect()
  77.         elif item == ITEM_CONNECT:
  78.             self.connect()
  79.  
  80.     def connect(self):
  81.         try:
  82.             interslip.connect()
  83.         except interslip.error, arg:
  84.             EasyDialogs.Message("Cannot connect: "+arg[1])
  85.  
  86.     def disconnect(self):
  87.         try:
  88.             interslip.disconnect()
  89.         except interslip.error, arg:
  90.             EasyDialogs.Message("Cannot disconnect: "+arg[1])
  91.             
  92.     def updatestatus(self):
  93.         try:
  94.             status, msgnum, msg = interslip.status()
  95.         except interslip.error, arg:
  96.             EasyDialogs.Message("Cannot get status: "+arg[1])
  97.             sys.exit(1)
  98.         if status == self.last_status and msgnum == self.last_msgnum:
  99.             return
  100.         self.last_status = status
  101.         self.last_msgnum = msgnum
  102.         if msgnum == 0:
  103.             msg = ''
  104.         
  105.         try:
  106.             txt = status2text[status]
  107.         except IndexError:
  108.             txt = "<unknown state %d>"%status
  109.  
  110.         tp, h, rect = self.wid.GetDialogItem(ITEM_STATUS)
  111.         Dlg.SetDialogItemText(h, txt)
  112.         
  113.         tp, h, rect = self.wid.GetDialogItem(ITEM_MESSAGE)
  114.         Dlg.SetDialogItemText(h, msg)
  115.  
  116. main()
  117.